library(ggplot2) library(geo) library(maps) library(mapdata)
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
d <- read.csv("http://www.hafro.is/~einarhj/data/minke.csv", stringsAsFactors = FALSE) d2 <- read.csv("http://data.hafro.is/assmt/2015/cod/summary.csv", stringsAsFactors = FALSE)
The minke data:
str(d)
The assessment summary data:
Taken from hafro data site
str(d2)
ggplot has three key components the user needs to specify in order to create a plot (there are lots more of components):
data: Has to be a dataframe
A set of aesthetic mappings [aes] between variables in the data and visual properties
At least one layer which describes how to render each observation. Layers are usually created with a geom function.
ggplot(data = d) # minke data
What do put on the x axis and what on the y-axis
ggplot(data = d, aes(x = age, y = length)) # minke data
The shorcut for layer
is geom_**
. Here we just want points:
ggplot(data = d, aes(x = age, y = length)) + geom_point()
... all road leads to Rome:
ggplot(d, aes(x = age, y = length)) + geom_point() ggplot(d, aes(age, length)) + geom_point() ggplot() + geom_point(data = d, aes(age, length)) ggplot(data = d) + geom_point(aes(x = age, y = length)) ggplot(d) + geom_point(aes(age, length)) d %>% ggplot() + geom_point(aes(age, length))
Can be stored as an object for later use:
p <- ggplot(d, aes(age, length)) + geom_point()
The class:
class(p)
The structure (a bit of latin - not run here):
str(p)
p <- ggplot(d) p + geom_point(aes(age, length, colour = sex)) p + geom_point(aes(age, length, colour = area))
Manual control of colours:
p <- ggplot(d) p + geom_point(aes(age, length, colour = sex)) + scale_colour_manual(values = c("orange","brown")) p + geom_point(aes(age, length, colour = area)) + scale_colour_manual(values = c("orange","brown"))
p + geom_point(aes(age, length, shape = sex))
p + geom_point(aes(age, length, size = stomach.volume))
To reveal overlays:
p + geom_point(aes(age, length, size = stomach.volume), alpha = 0.6) p + geom_point(aes(age, length, size = stomach.volume), alpha = 0.3, col = "red")
Display same graph by subsets of the data:
ggplot(d) + geom_point(aes(age, length, colour = sex)) + facet_wrap(~ area)
p <- ggplot(d, aes(age, length)) p + geom_point() + geom_smooth() p + geom_point() + geom_smooth(span = 0.1)
Specific models:
p + geom_point() + geom_smooth(method = "lm")
Add a little random noise to the data to avoid overplotting:
p <- ggplot(d, aes(sex, length)) p + geom_point() p + geom_jitter()
p + geom_boxplot() p + geom_violin()
p + geom_boxplot() + geom_jitter()
p <- ggplot(d, aes(length)) p + geom_freqpoly() p + geom_freqpoly(aes(colour = sex))
More details distribution than boxplots do, but more space:
p + geom_histogram() p + geom_histogram(aes(fill = sex))
Better way for histograms:
p + geom_histogram(aes(fill = sex)) + facet_wrap(~ sex, ncol = 1)
Controlling binwidths:
p + geom_histogram(binwidth = 50)
Histograms for discrete data:
ggplot(d, aes(maturity)) + geom_bar()
ggplot(d2, aes(Year, Rec)) + geom_line() ggplot(d2, aes(Year, Rec)) + geom_line() + expand_limits(y = 0)
ggplot(d2, aes(SSB, Fbar)) + geom_line() ggplot(d2, aes(SSB, Fbar)) + geom_path()
Check out difference of:
?geom_line ?geom_path
Of more use:
ggplot(d2, aes(SSB, Fbar)) + geom_path() + geom_point(aes(colour = Year), size = 3)
p <- ggplot(d, aes(age, length, colour = sex)) + geom_point() p p + labs(x = "Age [year]", y = "Length [cm]", colour = "Sex", title = "My minke plot")
Controls which values appear as tick marks
p <- ggplot(d, aes(age, length)) + geom_point() + labs(x = NULL, y = NULL) p p + scale_x_continuous(breaks = c(5, 10, 15, 20, 25, 30, 35, 40, 45)) p + scale_x_continuous(breaks = seq(5, 45, by = 5)) + scale_y_continuous(breaks = seq(500, 950, by = 50))
p <- ggplot(d, aes(maturity, length)) p + geom_jitter() p + geom_jitter() + ylim(600, 800) p + geom_jitter() + ylim(NA, 800) # setting only one limit
For descrete variables:
p + geom_jitter() + ylim(600,800) + xlim("immature","mature")
But be careful when using with summary statistics, e.g.:
p + geom_boxplot() p + geom_boxplot() + ylim(600, 800)
Remedy:
p + geom_boxplot() + coord_cartesian(ylim = c(600, 800))
str(island)
p <- ggplot(island, aes(lon, lat)) + labs(x = NULL, y = NULL) p + geom_point() p + geom_line()
p + geom_path() p + geom_path() + coord_map()
map_data
function.m <- map_data("world") str(m) ggplot(m) + geom_polygon(aes(long, lat, group = group)) + coord_quickmap()
The reslution of "word" is not very high as can be seen if only want denmark:
denmark <- map_data("world", region = "denmark") ggplot(denmark) + geom_polygon(aes(long, lat, group = group)) + coord_quickmap()
For higher resolution the object "worldHires" is often sufficient:
denmark <- map_data("worldHires", region = "Denmark") # here the capital ggplot(denmark) + geom_polygon(aes(long, lat, group = group)) + coord_quickmap()
Remedy (get rid of "colony"):
i <- denmark$long > 0 denmark <- denmark[i,] ggplot(denmark) + geom_polygon(aes(long, lat, group = group)) + coord_quickmap()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.